﻿@page
@model ScanQrCodeDemo.Web.Pages.LoginModel
@using ScanQrCodeDemo.Web.Utility
@inject RedisHelper redis
@{
    Guid guid = redis.CreateNew();
}

<style>
    .hide {
        display: none;
    }
</style>

<div class="row">
    <div class="col-md-5">
        <div class="card">
            <div class="card-header"></div>
            <div class="card-body">

            </div>
        </div>
    </div>

    <div class="col-md-5">
        <div class="card offset-1">
            <div class="card-header">扫码登录</div>
            <div class="card-body">
                <img class="img-thumbnail" src="/qrcode/@guid" style="width:300px;height:300px;" alt="验证码" />
            </div>
            <div class="card-footer">
                <div class="alert-info hide" id="alert-tips">扫码成功！请确认登录！</div>
            </div>
        </div>
    </div>

</div>


@section Scripts{
    <script>
        function checkLogin() {
            $.ajax({
                url: '/GetSession/' + '@guid',
                success: function (ret) {
                    console.log(ret);
                    if (ret.userId != null && ret.userId.length > 0) {

                        console.log('确认登录！跳转！');
                        location.href = '/LoginWithId?userId=' + ret.userId + '&id=@guid';

                    } else if (ret.hasScaned === true) {

                        $('#alert-tips').removeClass('hide');
                        console.log('扫码成功，请确认登录！');

                    } else {
                        console.log('wait for scan login...')
                    }
                }, error: function (ret) {
                    console.log(ret);
                }
            })
        }

        $(function () {
            setInterval(checkLogin, 1000);
        });
    </script>
}
